<template>
	<xn-form-container
		:title="formData.id ? '编辑文章' : '增加文章'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="文章标题：" name="title">
						<a-input v-model:value="formData.title" placeholder="请输入文章标题" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="分类：" name="category">
						<a-input v-model:value="formData.category" placeholder="请输入分类" allow-clear/>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="文章内容：" name="content">
						<a-input v-model:value="formData.content" placeholder="请输入文章内容" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否VIP观看：" name="vipStatus">
						<a-radio-group v-model:value="formData.vipStatus" placeholder="请选择是否VIP观看"
									   :options="vipStatusOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="消耗积分余额：" name="integral">
						<a-input-number v-model:value="formData.integral" :min="1" :max="10000" style="width: 100%"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="文章状态：" name="status">
						<a-radio-group v-model:value="formData.status" placeholder="请选择文章状态"
									   :options="statusOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="封面：" name="cover">
						<a-input v-model:value="formData.cover" placeholder="请输入封面" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否可评论：" name="flgComment">
						<a-radio-group v-model:value="formData.flgComment" placeholder="请选择是否可评论"
									   :options="flgCommentOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="排序号：" name="sort">
						<a-input-number v-model:value="formData.sort" :min="1" :max="10000" style="width: 100%"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="发布时间：" name="publishTime">
						<a-date-picker v-model:value="formData.publishTime" value-format="YYYY-MM-DD HH:mm:ss" show-time
									   placeholder="请选择发布时间" style="width: 100%"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="blogPostForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import blogPostApi from '@/api/biz/blogPostApi'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const userTypeOptions = ref([])
const vipStatusOptions = ref([])
const statusOptions = ref([])
const flgCommentOptions = ref([])

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	userTypeOptions.value = tool.dictList('USER_TYPE')
	vipStatusOptions.value = tool.dictList('YES_OR_NO')
	statusOptions.value = tool.dictList('AUDIT_STATUS')
	flgCommentOptions.value = tool.dictList('YES_OR_NO')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		blogPostApi
			.blogPostSubmitForm(formDataParam, formDataParam.id)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
